package com.shinhye.elephantsafe;

import android.app.Activity;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.TextView;

import com.shinhye.elephantsafe.db.dao.CommonNumberDao;

public class CommonNumberQueryActivity extends Activity {
	private ExpandableListView elv_commonnumber;
	private SQLiteDatabase db;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_commonnumber_query);
		
		String path = getFilesDir().getPath() + "/commonnum.db";
		db = SQLiteDatabase.openDatabase(path, null, SQLiteDatabase.OPEN_READONLY);
		elv_commonnumber = (ExpandableListView) findViewById(R.id.elv_commonnumber);
		MyExpandableListAdapter adapter = new MyExpandableListAdapter();
		elv_commonnumber.setAdapter(adapter);
		
		elv_commonnumber.setOnChildClickListener(new OnChildClickListener() {
			@Override
			public boolean onChildClick(ExpandableListView parent, View v,
					int groupPosition, int childPosition, long id) {
				String number = (String) v.getTag();
				Intent intent = new Intent(Intent.ACTION_DIAL); //直接拨打电话设置为Intent.ACTION_CALL。
				intent.setData(Uri.parse("tel:" + number));
				startActivity(intent);
				
				return true;
			}
		});
	}
	
	class MyExpandableListAdapter extends BaseExpandableListAdapter {
		@Override
		public int getGroupCount() {
			return CommonNumberDao.getMainCount(db);
		}

		@Override
		public int getChildrenCount(int groupPosition) {
			return CommonNumberDao.getSubCount(groupPosition + 1, db);
		}

		@Override
		public View getGroupView(int groupPosition, boolean isExpanded,
				View convertView, ViewGroup parent) {
			String name = CommonNumberDao.getMainName(groupPosition + 1, db);
			if(convertView == null) {
				convertView = View.inflate(CommonNumberQueryActivity.this, R.layout.item_commonnumber_expand, null);
			}
			TextView view = (TextView) convertView;
			view.setText(name);
			return view;
		}

		@Override
		public View getChildView(int groupPosition, int childPosition,
				boolean isLastChild, View convertView, ViewGroup parent) {
			String[] strs = CommonNumberDao.getSubName(groupPosition + 1, childPosition + 1, db);
			String name = strs[0];
			String number = strs[1];
			if(convertView == null) {
				convertView = new TextView(CommonNumberQueryActivity.this);
			}
			TextView view = (TextView) convertView;
			view.setText("\t" + name + "\n\t  " + number);
			view.setTextSize(15);
			view.setTag(number);
			return view;
		}

		@Override
		public boolean isChildSelectable(int groupPosition, int childPosition) {
			return true;
		}

		@Override
		public Object getGroup(int groupPosition) {
			return null;
		}

		@Override
		public Object getChild(int groupPosition, int childPosition) {
			return null;
		}

		@Override
		public long getGroupId(int groupPosition) {
			return 0;
		}

		@Override
		public long getChildId(int groupPosition, int childPosition) {
			return 0;
		}

		@Override
		public boolean hasStableIds() {
			return false;
		}
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
		db.close();
	}

}
